2020-2021 Sunseeker Telemetry and Lighting System
ref_ex1_internalReference.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //******************************************************************************
70 //******************************************************************************
71 
72 #define TIMER_PERIOD 80
73 
74 void main (void)
75 {
76  //Stop Watchdog Timer
77  WDT_A_hold(WDT_A_BASE);
78 
79  //Set P1.1 as an input pin.
80  /*
81 
82  * Select Port 1
83  * Set Pin 1 as input
84  * Set Ternary module function
85  */
86  GPIO_setAsPeripheralModuleFunctionInputPin(
87  GPIO_PORT_P1,
88  GPIO_PIN1,
89  GPIO_TERNARY_MODULE_FUNCTION);
90 
91  //Set P1.0 as an output pin.
92  /*
93 
94  * Select Port 1
95  * Set Pin 0 as output
96  */
97  GPIO_setAsOutputPin(
98  GPIO_PORT_P1,
99  GPIO_PIN0);
100 
101 
102  //Set P3.5 as an output pin.
103  /*
104 
105  * Select Port 3
106  * Set Pin 5 as output
107  */
108  GPIO_setAsOutputPin(
109  GPIO_PORT_P3,
110  GPIO_PIN5);
111 
112  //Initialize the ADC10 Module
113  /*
114  * Base Address for the ADC10 Module
115  * Use internal ADC10 bit as sample/hold signal to start conversion
116  * USE MODOSC 5MHZ Digital Oscillator as clock source
117  * Use default clock divider of 1
118  */
119  ADC10_B_init(ADC10_B_BASE,
120  ADC10_B_SAMPLEHOLDSOURCE_SC,
121  ADC10_B_CLOCKSOURCE_ADC10OSC,
122  ADC10_B_CLOCKDIVIDER_1);
123 
124  ADC10_B_enable(ADC10_B_BASE);
125 
126  /*
127  * Base Address for the ADC10 Module
128  * Sample/hold for 16 clock cycles
129  * Do not enable Multiple Sampling
130  */
131  ADC10_B_setupSamplingTimer(ADC10_B_BASE,
132  ADC10_B_CYCLEHOLD_16_CYCLES,
133  ADC10_B_MULTIPLESAMPLESDISABLE);
134 
135  //Configure Memory Buffer
136  /*
137  * Base Address for the ADC10 Module
138  * Use input A1
139  * Use positive reference of Internally generated Vref
140  * Use negative reference of AVss
141  */
142  ADC10_B_configureMemory(ADC10_B_BASE,
143  ADC10_B_INPUT_A1,
144  ADC10_B_VREFPOS_INT,
145  ADC10_B_VREFNEG_AVSS);
146 
147  ADC10_B_clearInterrupt(ADC10_B_BASE,
148  ADC10IE0);
149 
150  //Enable Memory Buffer interrupt
151  ADC10_B_enableInterrupt(ADC10_B_BASE,
152  ADC10IE0);
153 
154  //Configure internal reference
155  //If ref generator busy, WAIT
156  while ( REF_BUSY == Ref_isRefGenBusy(REF_BASE) ) ;
157 
158  //Select internal ref = 1.5V
159  Ref_setReferenceVoltage(REF_BASE,
160  REF_VREF1_5V);
161  //Internal Reference ON
162  Ref_enableReferenceVoltage(REF_BASE);
163 
164  // Configure TA0 to provide delay for reference settling ~75us
165  Timer_A_initUpModeParam param = {0};
166  param.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
167  param.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
168  param.timerPeriod = TIMER_PERIOD;
169  param.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
170  param.captureCompareInterruptEnable_CCR0_CCIE =
171  TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
172  param.timerClear = TIMER_A_DO_CLEAR;
173  param.startTimer = true;
174  Timer_A_initUpMode(TIMER_A0_BASE, &param);
175 
176  __bis_SR_register(CPUOFF + GIE); // LPM0, TA0_ISR will force exit
177 
178 
179 
180  for (;;)
181  {
182  //Delay between conversions
183  __delay_cycles(5000);
184 
185  //Enable and Start the conversion
186  //in Single-Channel, Single Conversion Mode
187  ADC10_B_startConversion(ADC10_B_BASE,
188  ADC10_B_SINGLECHANNEL);
189 
190  //LPM0, ADC10_ISR will force exit
191  __bis_SR_register(CPUOFF + GIE);
192  //For debug only
193  __no_operation();
194 
195 
196  }
197 }
198 
199 //ADC10 interrupt service routine
200 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
201 #pragma vector=ADC10_VECTOR
202 __interrupt
203 #elif defined(__GNUC__)
204 __attribute__((interrupt(ADC10_VECTOR)))
205 #endif
206 void ADC10_ISR (void)
207 {
208  switch (__even_in_range(ADC10IV,12)){
209  case 0: break; //No interrupt
210  case 2: break; //conversion result overflow
211  case 4: break; //conversion time overflow
212  case 6: break; //ADC10HI
213  case 8: break; //ADC10LO
214  case 10: break; //ADC10IN
215  case 12: //ADC10IFG0
216 
217  //Automatically clears ADC10IFG0 by reading memory buffer
218  //ADC10MEM = A0 > 0.5V?
219  if (ADC10_B_getResults(ADC10_B_BASE) < 0x155){
220  //Clear P1.0 LED off
221  /*
222 
223  * Select Port 1
224  * Set Pin 0 to output Low.
225  */
227  GPIO_PORT_P1,
228  GPIO_PIN0
229  );
230 
231  //Clear P3.5 LED off
232  /*
233 
234  * Select Port 3
235  * Set Pin 5 to output Low.
236  */
238  GPIO_PORT_P3,
239  GPIO_PIN5
240  );
241 
242  } else {
243  //Set P1.0 LED on
244  /*
245 
246  * Select Port 1
247  * Set Pin 0 to output high.
248  */
250  GPIO_PORT_P1,
251  GPIO_PIN0
252  );
253 
254  //Set P3.5 LED on
255  /*
256 
257  * Select Port 3
258  * Set Pin 5 to output high.
259  */
261  GPIO_PORT_P3,
262  GPIO_PIN5
263  );
264  }
265 
266  //Clear CPUOFF bit from 0(SR)
267  //Breakpoint here and watch ADC_Result
269  break;
270  default: break;
271  }
272 }
273 
274 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
275 #pragma vector=TIMER0_A0_VECTOR
276 __interrupt
277 #elif defined(__GNUC__)
278 __attribute__((interrupt(TIMER0_A0_VECTOR)))
279 #endif
280 void TA0_ISR (void)
281 {
282  TA0CTL = 0;
283  LPM0_EXIT; // Exit LPM0 on return
284 }
285 
MPU_initThreeSegmentsParam param
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)
void ADC10_ISR(void)
void main(void)
void TA0_ISR(void)
#define TIMER_PERIOD